<?php 
namespace Gigasweb\Wordpress\Metabox;
class Tables {
	private $_Name;
	private $_Columns;
	private $_Content;
	
	function __construct( $name, $columns = array( Array( 'name' => 'Column1' ), Array( 'name' => 'Column1' ) ), $content ) {
		$this->_Name = $name;
		$this->_Columns = $columns;
		$this->_Content = $content;
	}
	
	function render_table( $key, $title, $rows = array() ) {
		$cellsPerRow = count($this->_Columns);
		?>
	    <table class="table table-<?php echo $key; ?>">
	    	<tr><td colspan="100"><input class="title" name="<?php echo $this->_Name;?>[<?php echo $key; ?>][title]" placeholder="<?php echo __( "Titolo" ); ?>" value="<?php echo $title; ?>" /> <a href="javascript:addRow('<?php echo $key; ?>');" class="button"><?php echo __( "Aggiungi riga" ); ?></a>  <a href="javascript:;" class="deleteTable"><?php echo __( "Delete" ); ?></a></td></tr>
	    	<tr>
	    		<?php foreach( $this->_Columns as $c ) { ?>
	            	<th class="<?php isset( $c['class'] ) ? $c['class'] : ''; ?>"><?php echo $c['name']; ?></th>
	            <?php } ?>
	        </tr>
	        <?php if( is_array( $rows ) && count( $rows ) )  {
            	foreach( $rows as $rowKey => $row ) {
            		$this->render_table_row( $key, $rowKey, $row['cells'] );
            	}
            } ?>
	    </table>
	   <?php
	}
	
	function render_table_row( $key, $rowId, $values ) {
		?>
    	<tr>
        	<?php $i = 0; foreach( $values as $value ) { ?>
	        	<td><input class="cell <?php echo isset( $this->_Columns[$i] ) && isset( $this->_Columns[$i]['class'] ) ? $this->_Columns[$i]['class'] : ''; ?>" name="<?php echo $this->_Name;?>[<?php echo $key; ?>][rows][<?php echo $rowId; ?>][cells][]" placeholder="<?php echo __( "Inserisci un valore" ); ?>" value="<?php echo $value; ?>" /></td>
            <?php $i++; } ?>
            <td><a href="javascript:;" class="deleteRow"><?php echo __( "Delete" ); ?></a></td>
        </tr>
		<?php
	}
	
	public function Render() {
		?>
		<div class="">
	        <div>
	        	<a href="javascript:addTable();" class="button"><?php echo __( "Aggiungi tabella" ); ?></a>
	            <div id="wrapper-tables">
	            <?php
	            if( is_array( $this->_Content ) && count( $this->_Content ) ) {
					foreach( $this->_Content as $key=>$e ) {
						$this->render_table( $key, $e['title'], $e['rows'] );
					} 
				}
				?>
	            </div>
	        </div>
	        <script type="text/javascript">
			//<![CDATA[
				<?php
				ob_start();
				$this->render_table_row( '{n}', '{r}', array_fill( 0, count( $this->_Columns ), '' ) );
				$rowTemplate = ob_get_clean();
				
				ob_start();
				$this->render_table( '{n}', '' );
				$tableTemplate = ob_get_clean();
				?>
				jQuery(document).ready(function($){
					$('#wrapper-tables .deleteRow').live('click',function(){
						
						var p=$(this).parent().parent();
						p.remove();
					});
					
					$('#wrapper-tables .deleteTable').live('click',function(){
						
						var p=$(this).parent().parent().parent().parent();
						p.remove();
					});
				});
				function addTable() {
					var s='{n}';
					var templateTable=<?php echo json_encode( $tableTemplate ); ?>;
					var id=(new Date()).getTime();
					templateTable=templateTable.replace(/{n}/g,id);
					
					var newTable=jQuery(templateTable);
					
					jQuery('#wrapper-tables').append(newTable);
					addRow(id);
					addRow(id);
					addRow(id);
					addRow(id);				
				}
				
				function addRow(id) {
					var table=jQuery('#wrapper-tables .table-'+id);
					var s='{n}';
					var templateRow=<?php echo json_encode( $rowTemplate ); ?>;
					var rowId=(new Date()).getTime();
					templateRow=templateRow.replace(/{n}/g,id);
					templateRow=templateRow.replace(/{r}/g,rowId);
					
					
					table.append(templateRow);
				}
			//]]>
			</script>
		</div>     
	    <?php
	}
}